﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TesteSorteio
{
    public class Sorteio
    {   
        Texture2D textura1, textura2, textura3;
        Rectangle rectA = new Rectangle (0,0,50,50);
        Random randPosicao = new Random();
        List<Texture2D> Texturas;
        List<Rectangle> Retangulos;
        Random rndTexture;
        int valorSorteio, tempo_atual, tempo_maximo, fator_movimento;
        
        public Sorteio(ContentManager Content) 
        {
            Texturas = new List<Texture2D>();
            Retangulos = new List<Rectangle>();
            
            textura1 = Content.Load<Texture2D>("1");
            textura2 = Content.Load<Texture2D>("2");
            textura3 = Content.Load<Texture2D>("3");
            
            rndTexture = new Random();

            tempo_atual = 0;
            tempo_maximo = 1200;
            fator_movimento = 2;
        }
        public void Update(GameTime gameTime)
        {
            tempo_atual += gameTime.ElapsedGameTime.Milliseconds;

            if (Retangulos == null)
                return;

            if (tempo_atual == tempo_maximo)
            {
                ListaDesenho();
                tempo_atual = 0;
            }

            for (int g = 0; g < Retangulos.Count; g++)
                Retangulos[g] = new Rectangle((int)Retangulos.ElementAt(g).X,(int) Retangulos.ElementAt(g).Y + fator_movimento,
                (int)Retangulos.ElementAt(g).Width, (int)Retangulos.ElementAt(g).Height);

        }
        private Texture2D RetornaTextura(int valorTextura)
        {
            switch (valorTextura)
            {
                case 1:
                    return textura1;
                case 2:
                    return textura2;
                default:
                    return textura3;
           }
        }
        private Rectangle Posicao(int fatorPosicao)
        {
            rectA = new Rectangle(fatorPosicao * 200, -30, 50, 50);
            return rectA;
        }
        public void ListaDesenho ()
        {
            valorSorteio = rndTexture.Next(0, 3);
            Texturas.Add(RetornaTextura(valorSorteio));

            valorSorteio = rndTexture.Next(1, 4);
            Retangulos.Add(Posicao(valorSorteio));
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            if (Texturas == null)
                return;

            for (int i = 0; i < Texturas.Count; i++)
                spriteBatch.Draw(Texturas.ElementAt(i), Retangulos.ElementAt(i), Color.White);
   
            spriteBatch.End();
        }
    }
}
